package com.hongxun.budget.ui.activity.tk;

import android.graphics.Point;
import android.os.Bundle;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseActivity;
import com.hongxun.budget.utils.T;

import butterknife.BindView;
import butterknife.OnClick;

public class LocationDragActivity extends BaseActivity {

    @BindView(R.id.map)
    MapView mMapView;
    AMap aMap;

    @Override
    public  int getLayoutId() {
        return R.layout.activity_location_drag;
    }

    @Override
    public  void initView(Bundle savedInstanceState) {
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        init();
    }

    private Marker locationMarker;

    /**
     * 初始化
     */
    private void init() {
        aMap.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {
            }

            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {
                T.showLong(context,cameraPosition.target.toString());
            }
        });

        aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
            @Override
            public void onMapLoaded() {
                addMarkerInScreenCenter(null);
                //TODO 移动到当前点位的位置，用于修改点位的的经纬度。
//                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(obj.getDouble("latitude"), obj.getDouble("longitude")), 20);
//                aMap.animateCamera(cameraUpdate);
            }
        });
    }

    private void addMarkerInScreenCenter(LatLng locationLatLng) {
        LatLng latLng = aMap.getCameraPosition().target;
        Point screenPosition = aMap.getProjection().toScreenLocation(latLng);
        locationMarker = aMap.addMarker(new MarkerOptions()
                .anchor(0.5f, 0.5f)
                .icon(BitmapDescriptorFactory.fromResource(R.mipmap.purple_pin)));
        //设置Marker在屏幕上,不跟随地图移动
        locationMarker.setPositionByPixels(screenPosition.x, screenPosition.y);
        locationMarker.setZIndex(1);

    }



    @OnClick(R.id.right_save)
    public void onViewClicked() {
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public  void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public  void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }
}
